﻿using System;
using System.Collections.Generic;
using System.Text;

using MapCenterSystem.DataInfo;
using MapCenterSystem.DefineType;
using System.Data.SqlClient;
using MapCenterSystem.Configuration;
using System.Collections;
using System.Data;
using MapCenterSystem.Utility;

namespace MapCenterSystem.DataAccess
{
    public class HomeService : StreetServices
    {
        public IList<HomeInfoEx> FindExactAddress(string number, string street)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_Address_FindExactAddress";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@number", number);
                command.Parameters.AddWithValue("@street", street);

                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                IList<HomeInfoEx> result = new List<HomeInfoEx>();
                while (reader.Read())
                {
                    HomeInfoEx home = new HomeInfoEx();
                    home.HomeNumber = Convert.ToString(reader["Number"]);
                    home.Coordinate = new Coordinate(Convert.ToDouble(reader["xlocation"]), Convert.ToDouble(reader["ylocation"]));
                    home.Area = Convert.ToString(reader["Area"]);
                    result.Add(home);
                }
                return result;
            }
        }

        public IList<HomeInfoEx> FindNearAddress(string number, string street)
        {
            using (SqlConnection sqlConnect = new SqlConnection(GlobalConfig.ConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_Address_FindNearAddress";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@number", number);
                command.Parameters.AddWithValue("@area", street);

                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                IList<HomeInfoEx> result = new List<HomeInfoEx>();
                while (reader.Read())
                {
                    HomeInfoEx home = new HomeInfoEx();
                    home.HomeNumber = Convert.ToString(reader["Number"]);
                    home.Coordinate = new Coordinate(Convert.ToDouble(reader["xlocation"]), Convert.ToDouble(reader["ylocation"]));
                    home.Area = Convert.ToString(reader["Area"]);
                    result.Add(home);
                }
                return result;
            }
        }
    }
}
